<template>
    <div style="width: 800px" >
        <div id="editor"></div>
    </div>

</template>

<script>
    import '../../../utils/lazyload'
    export default {
        name: "editor",
        props:{
          code:{
              type:String,
              default:''
          }
        },
        data() {
            return {
                lastContent:"",
                mounted:false
            }
        },
        methods: {
            insertImage(url) {

            },
            insertVideo(url) {

            },
            initCode(code){
                if(!UM.getEditor || !UM.getEditor('editor')){
                    setTimeout(()=>{
                        this.initCode(code)
                    },100)
                }else{
                    UM.getEditor('editor').setContent(code);
                    setTimeout(()=>{
                        window.lazyload('',{selector:'[_src]'})
                    },500)
                }
            },
            initEditor(){
                if(!UM.getEditor){
                    setTimeout(()=>{
                        this.initEditor()
                    },100)
                    return;
                }
                let _this = this
                let um = UM.getEditor('editor');
                um.addListener( 'contentChange', ()=>{
                    this.$emit("update:code", um.getContent())
                })
                um.addListener( 'delkeyup', ()=>{
                    this.$emit("update:code", um.getContent())
                })
            }
        },
        beforeCreate(){
            let _this = this
            if(!window.UM || window.UM.I18N){
                window.UM = {I18N:{}};
            }
            this.createLink('https://geiligeili-1253592979.cos.ap-guangzhou.myqcloud.com/editor/themes/default/css/umeditor.css')
            this.createScript('https://geiligeili-1253592979.cos.ap-guangzhou.myqcloud.com/editor/umeditor.config.js', ()=>{})
            this.createScript('https://geiligeili-1253592979.cos.ap-guangzhou.myqcloud.com/editor/lang/zh-cn/zh-cn.js', ()=>{})
            this.createScript('https://geiligeili-1253592979.cos.ap-guangzhou.myqcloud.com/editor/umeditor.js', ()=>{_this.initEditor()})

        },
        mounted(){

        },
        destroyed() {
            // $('#summernote').summernote('destroy');
        }
    }
</script>

<style scoped>

</style>